package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.domain.Route;
import com.itheima.mapper.RouteMapper;
import com.itheima.service.RouteService;
import com.itheima.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class RouteServiceImpl implements RouteService {
    @Override
    public List<Route> findRouteByCid(Integer cid) {
        SqlSession sqlSession = MyBatisUtil.openSession();
        RouteMapper mapper = sqlSession.getMapper(RouteMapper.class);
        List<Route> routeList = mapper.findRouteByCid(cid);
        MyBatisUtil.closeSession(sqlSession);
        return routeList;
    }

    @Override
    public Route findRouteByRid(Integer rid) {
        SqlSession sqlSession = MyBatisUtil.openSession();
        RouteMapper mapper = sqlSession.getMapper(RouteMapper.class);
        Route route = mapper.findRouteByRid(rid);
        MyBatisUtil.closeSession(sqlSession);
        return route;
    }

    @Override
    public PageInfo<Route> findByPage(Integer pageNum, Integer pageSize, Integer cid) {
        PageHelper.startPage(pageNum, pageSize);
        SqlSession sqlSession = MyBatisUtil.openSession();
        RouteMapper mapper = sqlSession.getMapper(RouteMapper.class);
        List<Route> routeList = mapper.findRouteByCid(cid);
        return new PageInfo<>(routeList);
    }
}
